/**
 * AddBehaviorToBloonModel
 */

const conf = require('../../conf/conf.json');
const { Filter, translateFilters } = require('../../display/filters')
const { Display, translateValue, translateBloon } = require('../../display/general')
const { parseProperties } = require('../../util/enums')
const { getModelType } = require("../../util/utils")
const { parseEmission } = require("../emission/emission")

const models = {}

models.CarryProjectileModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('carryProjectile', 'carry projectile')
    const proj = parseProjectile(b.projectile)
    parseEmission(b.emission, proj, null)
    s.meta.projectile = proj
}

models.DamageOverTimeForTagModel = (b, s) => {
    s.name = translateValue('damageOverTime', 'damage over time')
    s.meta.damagePerTick = b.damage
    s.meta.damageType = b.immuneBloonProperties
    //s.meta.filters = translateFilters([new Filter(
        //`filterInTags`,
        //`can only affect ${b.tag}`,
        //translateBloon(b.tag)
    //)])
    s.meta.interval = Display.time(b.interval)
    if (b.triggerImmediate) {
        s.notes.push(translateValue('triggerImmediate', 'trigger immediately'))
    }
}

models.DamageOverTimeModel = (b, s) => {
    const { parseProjectileBehavior } = require("./behavior")
    let fakeProj = { damageMods: [] }
    if (b.damageModifierModels) {
        b.damageModifierModels.forEach(v => parseProjectileBehavior(v, fakeProj))
    }
    s.name = translateValue('damageOverTime', 'damage over time')
    s.meta.damagePerTick = b.damage
    s.meta.damageType = b.immuneBloonProperties
    s.meta.damageMods = fakeProj.damageMods.length > 0 ? fakeProj.damageMods : null
    s.meta.interval = Display.time(b.interval)
    if (b.triggerImmediate) {
        s.notes.push(translateValue('triggerImmediate', 'trigger immediately'))
    }
}

models.DesperadoMarkModel = (b, s) => {
    s.name = translateValue('desperadoMark', 'desperado mark')
    s.meta.cashMultiplier = Display.additivePercent(b.popCashMultiplier - 1)
    s.meta.desperadoDamageMultiplier = Display.multiplier(b.desperadoDamageMultiplier)
}

models.EmitOnDestroyModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('emitProjectileOnDestroy', 'emit projectile on destroy')
    const proj = parseProjectile(b.projectile)
    parseEmission(b.emission, proj, null)
    s.meta.projectile = proj
}

models.EmitOnPopModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('emitProjectileOnPop', 'emit projectile on pop')
    const proj = parseProjectile(b.projectile)
    parseEmission(b.emission, proj, null)
    s.meta.projectile = proj
}

models.GrowBlockModel = (b, s) => {
    s.name = translateValue('growBlock', 'blocks grows')
}

models.HauntModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('haunt', 'haunt')
    s.meta.projectile = parseProjectile(b.projectileOnDestroyModel)
}

models.HexModel = (b, s) => {
    s.name = translateValue('hex', 'hex')
    s.meta.damagePerTick = Display.percent(b.damagePercentOfMax)
    s.meta.interval = Display.time(b.interval)
}

models.IgnoreDmgImmunityModel = (b, s) => {
    s.name = translateValue('ignoreImmunity', 'ignore immunity')
    s.meta.bloonPropertiesToRemove = parseProperties(b.bloonPropertiesToRemove, conf.translate)
}

models.ProjectileOverTimeModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('projectileOverTime', 'projectile over time')
    const proj = parseProjectile(b.projectileModel)
    parseEmission(b.emissionModel, proj, null)
    s.meta.projectile = proj
    s.meta.interval = Display.time(b.interval)
}

models.UnstableConcoctionSplashModel = (b, s) => {
    const { parseProjectile } = require('./parse')
    s.name = translateValue('unstableConcoction', 'unstable concoction')
    const proj = parseProjectile(b.projectile)
    parseEmission(b.emissionModel, proj, null)

    proj.meta.damageToBloons = {}
    proj.meta.damageToBlimps = {}
    // override
    proj.damage = {}

    const arr = ['Moab', 'Bfb', 'Zomg', 'Ddt', 'Bad']
    arr.forEach(e => {
        proj.meta.damageToBloons[e] = b.baseIdToBloonDmg[e] ? b.baseIdToBloonDmg[e] : b.defaultBloonDmg
        proj.meta.damageToBlimps[e] = b.baseIdToMoabDmg[e] ? b.baseIdToMoabDmg[e] : b.defaultMoabDmg
    })
    proj.meta.damageToBloons.Boss = b.defaultBloonDmg
    proj.meta.damageToBlimps.Boss = b.bossToMoabDmg
    s.meta.projectile = proj
}

function parseBloonBehavior(b, status, type = null) {
    if (!type) type = getModelType(b)
    if (models[type]) models[type](b, status)
    else status.behaviors.push(b)
}

module.exports = {
    parseBloonBehavior
}